#ifndef MUTEX_H
#define MUTEX_H

#include "embed.h"

#if defined(AE_NO_THREAD)
#ifndef __BIT64__
typedef long mutex_t;
#else
typedef long long mutex_t;
#endif
#elif defined(AE_WIN32)
typedef CRITICAL_SECTION mutex_t;
#else
typedef pthread_mutex_t mutex_t;
#endif

static inline void mutex_init(mutex_t* m)
{
#if defined(AE_NO_THREAD)
	*m = 0;
#elif defined(AE_WIN32)
	InitializeCriticalSection(m);
#else
	pthread_mutex_init(m, NULL);
#endif
}

static inline void mutex_destory(mutex_t* m)
{
#if defined(AE_NO_THREAD)
	*m = 0;
#elif defined(AE_WIN32)
	DeleteCriticalSection(m);
#else
	pthread_mutex_destroy(m);
#endif
}

static inline void mutex_lock(mutex_t* m)
{
#if defined(AE_NO_THREAD)
	++ (*m);
#elif defined(AE_WIN32)
	EnterCriticalSection(m);
#else
	pthread_mutex_lock(m);
#endif
}

static inline void mutex_unlock(mutex_t* m)
{
#if defined(AE_NO_THREAD)
	-- (*m);
#elif defined(AE_WIN32)
	LeaveCriticalSection(m);
#else
	pthread_mutex_unlock(m);
#endif
}

#endif
